<?php 
# $Id$
require_once(APP_LIB_DIR . 'Sajax.php');
require_once('utils.inc.php');

function getlist($p='',$search='',$by='1',$s='DESC') { 
    $a = new TemplateEditorAction();
    parse_str($search,$res);
    return $a->getlist($p,$res,$by,$s); 
}
function lookup($what=''){
    $l = new TemplateEditorAction();
    return $l->lookup($what);
}
function save($detail='',$id='',$action=''){
    $s = new TemplateEditorAction();
    parse_str($detail,$res);
    return $s->save($res,$id,$action);
}

sajax_init();
//$sajax_debug_mode = 1;
sajax_export('getlist');
sajax_export('lookup');
sajax_export('save');
sajax_handle_client_request();
$sajax_request_type = 'POST';
require_once('header.php'); 
?>
<script language="JavaScript" type="text/javascript">

<?php sajax_show_javascript();?>

var s = "ASC";
var b = 1;

function page(p) {
    st = $('form1').toQueryString();
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function sort(by){
    s = (s=="DESC") ? "ASC" : "DESC";
    st = $('form1').toQueryString();
    p = $('pagenum');
    p = (p) ? p.value : 1;
    b = by;
    load();
    x_getlist(p,st,b,s,getlist_cb)
}
function filter(){
    st = $('form1').toQueryString();
    p = 1
    //st = Json.toString(st);
    load();
    x_getlist(p,st,b,s,getlist_cb);
}
function getlist_cb (new_data){
    //alert(new_data)
    data = Json_eval(new_data);
    var fields = [ ["id","ID"],
                   ["name","Message"],
                   ["type","Type"]
		  	
                 ]
    var  extra = [ [true,'id','edit'],
                   [false,'id','del']
                 ]
                 
    $("thetable").innerHTML = data[1]
    $("thetable").innerHTML += mytable(data[0],fields, extra)
}

/*function levels_cb (new_data){
    data = Json_eval(new_data);
    AddToOptionList(data, $('level'), 'id','name')
}*/
function save () {
    data = $('form2').toQueryString();
    id = $('myid').value
    $('myid').value = ''
    x_save(data,id, function(new_data1){
      Json_eval(new_data1);
      filter();
      show_t()
    });
}
function show_t(){
    if($('thetable').style.display == "none"){
        hide_t('newform','thetable')
        $('altfrm').value = "New Template"
    } else {
        hide_t('thetable','newform')
        $('altfrm').value = "Back to List"
    }
    $('form2').reset();
}

function edit (id) {
    x_save('',id,'get', function(new_data1){
       d = Json_eval(new_data1);
       $('name').value = d[0]['name'];
       $('type').value = d[0]['type'];
       $('myid').value = d[0]['id'];
       //optionSelected($('level'), d[0]['level_id'])
       hide_t('thetable','newform');
    }) 
    return false;
}

function del (id) {
    if (confirm('Are you sure you want to delete this Record?')){
      x_save('',id,'del', function(new_data1){
        Json_eval(new_data1);
        filter();
      })   
    }
}

function load (){
    //$("thetable").empty().addClass('ajax-loading');
    $("thetable").innerHTML = loading;
}
    function update() {
       var old = $('counter').value;
       $('counter').value=$('name').value.length;
       if($('counter').value > 160 && old <= 160) {
         alert('Too much data in the text box!');
         if(document.styleSheets) {
           $('counter').style.fontWeight = 'bold';
           $('counter').style.color = '#ff0000'; } }
       else if($('counter').value <= limit && old > limit
	       && document.styleSheets ) {
           $('counter').style.fontWeight = 'normal';
           $('counter').style.color = '#000000'; } 
       }

    function textCounter(field, countfield, maxlimit) {
        if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        // otherwise, update 'characters left' counter
        else
        countfield.value = maxlimit - field.value.length;
    }

    function CheckKey(e)
    {
        var unicode = e.keyCode? e.keyCode: e.charCode
        if (unicode == 60) { e.preventDefault(); }
    }
    function CheckCtrl(e,r)
    {
        var unicode = e.keyCode? e.keyCode: e.charCode
        if (unicode == 17) { document.Form1.send_Message.focus(); }
    }

</script>
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/structure2.css" type="text/css" />
<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/form2.css" type="text/css" />
<!--<link rel="stylesheet" href="<?=WEB_MODULE_DIR?>css/theme2.css" type="text/css" />-->
<table width="100%" class="menubar" cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td class="menudottedline" width="40%">
            <div class="pathway"><font size="2px" color="#000033"><b><?=$config['version']?></b></font></div>       
        </td>
        <td class="menudottedline" align="right">
            <table cellpadding="0" cellspacing="0" border="0" id="toolbar">
                <tr height="60" valign="middle" align="center">
                    <td>
                        <a class="toolbar" href="#">
                        <img src="<?=WEB_MODULE_DIR?>images/help_f2.png" 
                             align="middle" name="new" border="0"><br>Help
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>
<br>
<br>
<div align="center" class="centermain">
    <div class="main">
        <table class="adminheading">
            <tr>
                <th class="cpanel" rowspan="2" nowrap>Template Editor&nbsp;<font size="1px" color="#CCCCCC">[<?=strtoupper($auth['privilege'])?>]</font></th>
            </tr>
        </table>
    </div>
        <table class="adminlist">
          <tr>
            <th class="title"><a href="javascript: show_tool('panel');">:: SEARCH PANEL</a></th>
          </tr>
          <tr>
            <td width="40%" valign="top">
            <!-- filter panel -->
		  <div id="panel">
		    <table class="adminlist">
		      <tr>
		        <td>
			  <form id="form1" class="wufo" action="" method="post" onSubmit="filter();return false;">
			    <ul>
			      <li>
			        <span>
			          <select class="field select" style="width:10em" name="search[searchfield]">
		                    <option value="name">Message</option>
                                    <option value="type">Type</option>
			          </select>
                                </span>

                                <span>
                                  <input class="field text" size="15" name="search[searchtext]" value="" onKeyup="filter()" />
                                </span>

                                <span>
                                  <input id="saveForm" class="btTxt" type="button" value="Search" onClick="filter()"/>
                                  <input class="btTxt" type="button" value="Clear" onClick="this.form.reset();filter()"/>
                                  <input id="altfrm" class="btTxt" type="button" value="New Template" onClick="show_t()"/>
                                </span>
                              </li>
                            </ul>
                          </form>
			</td>
		      </tr>
		    </table>
		  </div>
			<!-- filter panel end -->
		</td>
	    </tr>
	</table>
        <table class="adminform">
            <tr>
                <th class="title">:: SEARCH RESULTS</th>
            </tr>
            <tr>
              <td>
                      <div id="thetable">
                        <img src="/mcs/modpub/images/loading_small.gif"/>
                      </div>
              </td>
	    </tr>
        </table>
        <table> 
            <tr>
              <td align="center">
                <div id="newform" style="display: none" class="container">
                  <h1>New</h1>
                        <form id="form2" class="wufoo" method="post" onSubmit="return false;">
                          <input type="hidden" value="" id="myid" />
                            <div class="info">
                              <h2>New Template</h2>
                                <p>Create new Template</p>
                            </div>
                              <ul>
                                <li>
                                  <label class="desc">Message</label>
                                    <div>
                                    <textarea name="details[name]" rows="2" cols="20" id="name" onkeyup="textCounter($('name'),$('counter'),160);" onchange="textCounter($('name'),$('counter'),160);" onkeypress="CheckKey(event)" onkeydown="CheckCtrl(event,this)" style="height: 90px; width: 300px;"></textarea>
                                    </div>
                                     <div>
                                     <label style="color: rgb(109, 107, 108);">Message Count</label>&nbsp;&nbsp;
                                     <input name="counter" value="160" id="counter" disabled="disabled" style="width: 60px;" type="text"></div>&nbsp;&nbsp;
                                </li>
                                <li>
                                  <label class="desc">Type</label>
                                    <div>
                                      <select class="field select" style="width:4" id="type" name="details[type]">
                                        <option value='DEFAULT'>DEFAULT</option>
                                        <option value='MEMO'>MEMO</option>
                                        <option value='GREET'>GREET</option>
                                        <option value='EMP'>EMP</option>
                                        <option value='RELOAD'>RELOAD</option>
                                      </select>
                                    </div>
                                </li>
                                <li>
                                  <span>
                                    <input id="saveForm" class="btTxt" type="button" value="Save" onClick="save();"/>
                                    <input id="saveForm" class="btTxt" type="reset" value="Clear" onClick=""/>
                                    <input id="saveForm" class="btTxt" type="button" value="Cancel" onClick="hide_t('newform', 'thetable')"/>
                                  </span>
                                </li>
                              </ul>
                        </form>
                    </div>
              </td>
            </tr>
        </table>
<script type="text/javascript">
x_getlist(getlist_cb)
//x_lookup(levels_cb)
</script>

<?php require_once('footer.php'); ?>
